package cn.edu.sgu.www.mhxysy.controller.achievement;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.achievement.RoleChengweiDTO;
import cn.edu.sgu.www.mhxysy.entity.achievement.RoleChengwei;
import cn.edu.sgu.www.mhxysy.pager.achievement.RoleChengweiPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.achievement.RoleChengweiService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "角色称谓管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/role_chengwei", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class RoleChengweiController {

	private final RoleChengweiService roleChengweiService;

	@Autowired
	public RoleChengweiController(RoleChengweiService roleChengweiService) {
		this.roleChengweiService = roleChengweiService;
	}

	@ApiOperation("更换角色称谓显示")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "roleId", value = "角色ID", required = true),
			@ApiImplicitParam(name = "chengweiId", value = "称谓ID", required = true)
	})
	@RequestMapping(value = "/change", method = RequestMethod.POST)
	public JsonResult<Void> change(
			@NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId,
			@NotNull(message = "称谓" + CustomMessages.ID_NOT_NULL) Integer chengweiId) {
		roleChengweiService.change(roleId, chengweiId);

		return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
	}

	@ApiOperation("添加角色称谓")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated RoleChengweiDTO roleChengweiDTO) {
		roleChengweiService.insert(roleChengweiDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID批量删除角色称谓")
	@RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
	public JsonResult<Void> deleteByIds(
			@ApiParam(name = "ids", value = "角色称谓ID列表", required = true)
			@RequestParam("ids") List<String> ids) {
		roleChengweiService.deleteByIds(ids);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改角色称谓信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated RoleChengweiDTO roleChengweiDTO) {
		roleChengweiService.updateById(roleChengweiDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("查询角色称谓")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "roleId", value = "角色ID", required = false),
			@ApiImplicitParam(name = "useStatus", value = "使用状态", required = false),
			@ApiImplicitParam(name = "chengweiId", value = "称谓ID", required = false)
	})
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	public JsonResult<RoleChengwei> select(
			@RequestParam(required = false) String roleId,
			@RequestParam(required = false) Integer useStatus,
			@RequestParam(required = false) Integer chengweiId) {
		RoleChengwei roleChengwei = roleChengweiService.select(roleId, useStatus, chengweiId);

		return JsonResult.success(null, roleChengwei);
	}

	@ApiOperation("分页查询角色称谓列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
	public JsonResult<JsonPage<RoleChengwei>> selectByPage(RoleChengweiPager pager) {
		Page<RoleChengwei> page = roleChengweiService.selectByPage(pager);

		return JsonResult.restPage(page);
	}

}